package com.pingan.project.autosendsms;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.lsh.lshrecyclerviewadapter.BaseAdapter;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = MainActivity.class.getSimpleName();
    private LinearLayout mActivityMain;
    private RadioGroup mRgSim;
    private EditText mEtKey;
    private EditText mEtContent;
    private Button mEtSave;
    private int simType;
    private RecyclerView list;
    private List<SmSBean> mDataList = new ArrayList<>();
    private SMSAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//        getSmsInfo(0, "555你好");
        list = (RecyclerView) findViewById(R.id.list);
        mActivityMain = (LinearLayout) findViewById(R.id.activity_main);
        mRgSim = (RadioGroup) findViewById(R.id.rg_sim);
        mEtKey = (EditText) findViewById(R.id.et_key);
        mEtContent = (EditText) findViewById(R.id.et_content);
        mEtSave = (Button) findViewById(R.id.et_save);
        mEtSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String key = mEtKey.getText().toString().trim();
                String content = mEtContent.getText().toString().trim();
                SmSBean bean = new SmSBean(key, content);
                init();
                mDataList.add(bean);
                ShareUtil.getInstance(MainActivity.this).saveSMS(mDataList);
                mAdapter.notifyDataSetChanged();
                mEtKey.setText("");
                mEtContent.setText("");
            }
        });
        mRgSim.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_1:
                        simType = 0;
                        ShareUtil.getInstance(MainActivity.this).saveSMSType(simType);
                        break;
                    case R.id.rb_2:
                        simType = 1;
                        ShareUtil.getInstance(MainActivity.this).saveSMSType(simType);
                        break;
                    default:
                        ShareUtil.getInstance(MainActivity.this).saveSMSType(simType);
                        break;
                }
            }
        });
        list.setLayoutManager(new LinearLayoutManager(this));
        init();
        list.setAdapter(mAdapter = new SMSAdapter(this, mDataList));
        mAdapter.setOnItemLongClickListener(new BaseAdapter.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(final int pos) {
                AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
                alert.setTitle("是否删除");
                alert.setMessage("");
                alert.setNegativeButton("否", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                alert.setNeutralButton("是", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        mDataList.remove(pos);
                        mAdapter.notifyDataSetChanged();
                        ShareUtil.getInstance(MainActivity.this).saveSMS(mDataList);
                    }
                });
                alert.create().show();
                return true;
            }
        });
        registerReceiver(sendMessage, new IntentFilter(SmsReciver.SENT_SMS_ACTION));
        registerReceiver(test, new IntentFilter("test"));
    }

    private void init() {
        mDataList.clear();
        List<SmSBean> sss = ShareUtil.getInstance(MainActivity.this).getSMS();
        mDataList.addAll(sss);
    }

    BroadcastReceiver sendMessage = new BroadcastReceiver() {

        @Override
        public void onReceive(Context c, Intent intent) {
            //判断短信是否发送成功
            switch (getResultCode()) {
                case Activity.RESULT_OK:
                    Log.e(TAG, "短信发送成功");
                    Toast.makeText(c, "短信发送成功", Toast.LENGTH_SHORT).show();
                    break;
                default:
                    Log.e(TAG, "发送失败");
                    Toast.makeText(c, "发送失败", Toast.LENGTH_LONG).show();
                    break;
            }
        }
    };

//    public void Test(View view) {
//        sendBroadcast(new Intent("123456"));
//    }

    BroadcastReceiver test = new TestBroadCast();

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(sendMessage);
    }
}
